﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Database.Classes;
using SD.Database;

namespace smallDeimos.Game.GameTools
{
    public partial class GameTool : System.Web.UI.MasterPage
    {
        public string User { get; set; }
        public int UserId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Label2.Text = DateTime.Now.ToString("G");
                if (Request.Cookies["sd_user"] != null && Request.Cookies["sd_id"] != null)
                {
                    HttpCookie cookie = Request.Cookies["sd_user"];
                    HttpCookie cookie2 = Request.Cookies["sd_id"];
                    User = cookie.Value;
                    UserId = int.Parse(cookie2.Value);
                }
                //if (Request.Cookies["User"] != null)
                //    User = Server.HtmlEncode(Request.Cookies["User"].Value);
                //else
                //    Response.Redirect("~/Error.aspx");

                //if (Request.Cookies["UserID"] != null)
                //    UserId = int.Parse(Server.HtmlEncode(Request.Cookies["userName"].Value));
                //else
                //    Response.Redirect("~/Error.aspx");
                //User = Session["User"].ToString();
                //UserId = int.Parse(Session["UserID"].ToString());

                if (User == null || User.ToString().Equals(string.Empty))
                    Response.Redirect("Default.aspx");

                string artifact = UserFunctions.UsedArtifact(UserId);

                int prod = UserFunctions.GetPlayerProductionPerH(UserId);
                double prodBonus = UserFunctions.GetBonusForProduction(UserId);

                int sci = UserFunctions.GetPlayerSciencePerH(UserId);
                double sciBonus = UserFunctions.GetBonusForScience(UserId);

                int cul = UserFunctions.GetPlayerCulturePerH(UserId);
                double culBonus = UserFunctions.GetBonusForCulture(UserId);

                int timeZone = UserFunctions.GetPlayerInt(UserId, "TimeZone");
                if (timeZone > 0)
                    Label1.Text = "+" + timeZone.ToString();
                else
                    Label1.Text = timeZone.ToString();

                lblNumberOfPlanets.Text = UserFunctions.PlayerPlanets(UserId).ToString() + "/" + UserFunctions.PlayerCulture(UserId).ToString();
                if ((int)(prod * (1 + prodBonus)) >= 0)
                    lblProduction.Text = String.Format("{0} + {1}% = {2}pp/h", prod, (int)(100 * prodBonus), (int)(prod * (1 + prodBonus)));
                else
                    lblProduction.Text = String.Format("{0} {1}% = {2}pp/h", prod, (int)(100 * prodBonus), (int)(prod * (1 + prodBonus)));

                if ((int)(cul * (1 + culBonus)) >= 0)
                    lblCulture.Text = String.Format("{0} + {1}% = {2}cul/h", cul, (int)(100 * culBonus), (int)(cul * (1 + culBonus)));
                else
                    lblCulture.Text = String.Format("{0} {1}% = {2}cul/h", cul, (int)(100 * culBonus), (int)(cul * (1 + culBonus)));

                if ((int)(sci * (1 + sciBonus)) >= 0)
                    lblScience.Text = String.Format("{0} + {1}% = {2}sci/h", sci, (int)(100 * sciBonus), (int)(sci * (1 + sciBonus)));
                else
                    lblScience.Text = String.Format("{0} {1}% = {2}sci/h", sci, (int)(100 * sciBonus), (int)(sci * (1 + sciBonus)));

                if (artifact == string.Empty)
                    lblArtifact.Text = "You don't use any artifact";
                else
                    lblArtifact.Text = "You use: " + artifact;
                if (lblArtifact.Text != string.Empty)
                    lblArtifact.ToolTip = ArtifactsFunctions.ArtifactDescription(lblArtifact.Text);
            }
            catch (Exception ex)
            {
                Session.Add("Error", ex.ToString());
                Response.Redirect("~/Error.aspx");
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                Request.Cookies.Clear();
                Response.Redirect("~/Default.aspx");
            }
            catch (Exception)
            { }
        }
    }
}
